package com.bq.gametrade.view.popwindow;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bq.gametrade.R;

import java.util.List;

/**
 * Created by Feel on 2017/2/22.
 */

public class BankNameSelectPop extends PopupWindow {


    private Activity activity;
    private List<String> list;
    private LinearLayout layHome;
    private ListView listView;
    private TextView tvDismiss;

    public interface OnSelectBankNameListener {
        void selectBankName(int position);
    }
    public BankNameSelectPop(Activity activity, List<String> list){
        super(activity);
        this.activity = activity;
        this.list = list;
        View view = LayoutInflater.from(activity).inflate(R.layout.pop_bankname_select, null);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
        this.setContentView(view);
//        this.setAnimationStyle(R.style.PayPasswordPopAnimation);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));
        initView(view);

    }
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);

    }

    private void initView(View view){
        listView = (ListView) view.findViewById(R.id.lv_bankNameSelect);
        listView.setAdapter(new BankNameSelectAdapter());
        tvDismiss = (TextView) view.findViewById(R.id.tv_dismiss);
        layHome = (LinearLayout) view.findViewById(R.id.lay_home);
        layHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setSelectBankNameListener(final OnSelectBankNameListener listener){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listener.selectBankName(position);
                dismiss();
            }
        });
    }

    class BankNameSelectAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(activity).inflate(R.layout.item_pop_banknameselect_listview, null);
                viewHolder.TvBanName = (TextView) convertView.findViewById(R.id.tv_bankName);


                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.TvBanName.setText(list.get(position));

            return convertView;
        }

        class ViewHolder {
            TextView TvBanName;


        }
    }
}
